using System.Collections.Generic;
using System.Threading.Tasks;
using Admin.NET.Application.AttributeInfo;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Application.Service.Tenant.Dto;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc;

namespace Admin.NET.Application.Service.Tenant;

public class SysTenantService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<SysTenant> _rep;

	private readonly UserManager _userManager;

	public SysTenantService(SqlSugarRepository<SysTenant> rep, UserManager userManager)
	{
		_rep = rep;
		_userManager = userManager;
	}

	/// <summary>
	///
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/tenant/volumn")]
	[OperationLog("租户扩容", "name")]
	public async Task SetVolumn(TenantVolumnSet input)
	{
		SysTenant entity = await _rep.GetFirstAsync((SysTenant u) => u.Id == input.Id);
		if (entity == null)
		{
			throw Oops.Oh("当前信息不存在，请联系管理员");
		}
		if (input.IsSubscribe)
		{
			entity.VolumeEnd = input.VolumeEnd;
			entity.VolumeStart = input.VolumeStart;
			entity.AllVolume = input.AllVolume;
		}
		else
		{
			entity.VolumeEnd = null;
			entity.VolumeStart = null;
			entity.AllVolume = 0;
		}
		entity.IsSubscribe = input.IsSubscribe;
		await _rep.AsUpdateable(entity).UpdateColumns((SysTenant u) => new { u.VolumeEnd, u.AllVolume, u.VolumeStart, u.IsSubscribe }).ExecuteCommandAsync();
	}

	/// <summary>
	/// 设置手续费
	/// </summary>
	/// <param name="rate"></param>
	/// <returns></returns>
	[HttpPost("/tenant/setrate")]
	[OperationLog("手续费设置", "name")]
	public async Task SetRate([FromQuery] decimal rate)
	{
		if (rate < 0m)
		{
			throw Oops.Bah("手续费范围的设置请>=0‰");
		}
		rate /= 1000m;
		await (from it in _rep.Context.Updateable<SysTenant>().SetColumns((SysTenant it) => it.Rate == rate)
			where it.Id == _userManager.TenantId
			select it).ExecuteCommandAsync();
	}

	/// <summary>
	/// 获取服务费费率
	/// </summary>
	/// <returns></returns>
	[HttpGet("/tenant/getrate")]
	public async Task<decimal> GetRate()
	{
		return await (from f in _rep.AsQueryable()
			where f.Id == _userManager.TenantId
			select f.Rate).FirstAsync() * 1000m;
	}

	/// <summary>
	/// 获取服务费率-业主端
	/// </summary>
	/// <returns></returns>
	[HttpGet("/tenant/mpgetrate")]
	public async Task<decimal> GetMpRate()
	{
		return await (from f in _rep.AsQueryable()
			where f.Id == _userManager.TenantId
			select f.Rate).FirstAsync();
	}

	[HttpPost("/tenant/setwechat")]
	public async Task SetTenantWeChat([FromBody] SetQRCodeInput input)
	{
		await (from u in _rep.Context.Updateable<SysTenant>().SetColumns((SysTenant u) => u.QRCodeUrl == input.QRCodeUrl).SetColumns((SysTenant u) => u.IsQRCodeCustom == input.IsQRCodeCustom)
			where u.Id == input.Id
			select u).ExecuteCommandAsync();
	}

	/// <summary>
	/// 获取所有品牌型号
	/// </summary>
	/// <param name="tenantId"></param>
	/// <returns></returns>
	[HttpGet("/tenant/getallbrandmodel")]
	public async Task<dynamic> GetAllBrandModel()
	{
		return await (from r in _rep.Context.Queryable<BrandModel>()
			select new
			{
				BrandName = r.Name,
				Type = r.Type,
				BrandId = r.Id
			}).ToListAsync();
	}

	/// <summary>
	/// 获取租户所属的品牌型号
	/// </summary>
	/// <returns></returns>
	[HttpGet("/tenant/gettenantbrandmodel")]
	public async Task<dynamic> GetTenantBrandModel([FromQuery] long tenantId)
	{
		return await (from l in _rep.Context.Queryable<TenantBrandModel>().LeftJoin((TenantBrandModel l, BrandModel r) => l.BrandId == r.Id)
			where l.TenantId == tenantId
			select l).Select((TenantBrandModel l, BrandModel r) => new TenantBrandModelOut
		{
			BrandName = r.Name,
			Type = l.Type,
			BrandId = l.BrandId
		}).ToListAsync();
	}

	/// <summary>
	/// 设置租户的品牌型号
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/tenant/settenantbrandmodel")]
	public async Task SetTenantBrandModel(TenantBrandModelInput input)
	{
		await (from w in _rep.Context.Deleteable<TenantBrandModel>()
			where w.TenantId == input.TenantId
			select w).ExecuteCommandAsync();
		List<TenantBrandModel> entity = await (from w in _rep.Context.Queryable<BrandModel>()
			where input.TenantBrandIds.Contains(w.Id)
			select new TenantBrandModel
			{
				TenantId = input.TenantId,
				Type = (int)w.Type,
				BrandId = w.Id
			}).ToListAsync();
		await _rep.Context.Insertable(entity).ExecuteCommandAsync();
	}
}
